package com.cht.commonUtil.infra;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cht.commonUtil.jqGrid.ResultTableSet;
import com.cht.commonUtil.jqGrid.Rows;
import com.cht.commonUtil.webInit.SP;
import com.cht.commonUtil.widgets.Widgets;
import com.google.gson.Gson;

public class QuerySquad extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding( SP.UIcodeSet );
		response.setContentType("application/json");
		response.setCharacterEncoding( SP.UIcodeSet );		
		
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		String subsysId = request.getParameter("subsysId");
		ResultTableSet resultTableSet = new ResultTableSet();
		
		String sql = "SELECT COUNT(*) FROM squad WHERE subsysid=?";
		String sord = request.getParameter("sord");
		String sidx =  request.getParameter("sidx");
		int total_pages = 1;
		int count = 0; 
		int page = 1;
		int limit = 10;
		if ("".equalsIgnoreCase(sidx)|| sidx==null|| "my_checkbox".equalsIgnoreCase(sidx)) {
			sidx = "squadId";
		} 
		if ("".equalsIgnoreCase(sord)|| sord==null) {
			sord = "ASC";
		} else {
			page = Integer.parseInt(request.getParameter("page"));
			limit = Integer.parseInt(request.getParameter("rows"));
		}
		
		try {
			conn = Widgets.getConn();	
			ps = conn.prepareStatement(sql);
			ps.setString(1, subsysId);
			rs = ps.executeQuery();
			rs.next();
			count  = rs.getInt(1);
			if (rs != null) {rs.close();}
			if (ps != null) {ps.close();}
			if ( count != 0 ) {
				
				if (limit  < count) {
					total_pages = (count / limit)+1;	
				}
				ArrayList<Rows> rows = new ArrayList<Rows>();
				
				sql = "SELECT squadId,squadPath FROM squad WHERE subsysId=? ORDER BY "+sidx+" "+sord+" LIMIT "+((limit*page-limit)<0?0:(limit*page-limit))+","+ limit;
				ps = conn.prepareStatement(sql);
				System.out.println(sql);
				ps.setString(1, subsysId);					
				rs = ps.executeQuery();
				
				while ( rs.next() ) {
					final String squadId = rs.getString("squadid"); 
					final Rows row = new Rows();
					final String[] cell = new String[2];
					row.setId( squadId );
					cell[0] = squadId;
					cell[1] = rs.getString("squadpath");
					
					row.setCell(cell);
					rows.add(row);
				}
				resultTableSet.setRows(rows);
			} else {
				page =0;
				total_pages=0;
			}
			resultTableSet.setPage(String.valueOf(page));
			resultTableSet.setTotal(total_pages);
			resultTableSet.setRecords(String.valueOf(count));
		} catch (Exception e) {
			Widgets.errLog(e);
		} finally {
			try {
				if (rs != null) {rs.close();}
				if (ps != null) {ps.close();}
				if (conn != null) {conn.close();}
			} catch (SQLException e) { e.printStackTrace(); }
		}
		
		response.getWriter().write(new Gson().toJson(resultTableSet));
	}

}
